#define _CRT_SECURE_NO_WARNINGS 1

class Solution
{
    int num;
    int left = 0, right = 0;
    vector<string> ret;
    string path;
public:
    vector<string> generateParenthesis(int n)
    {
        num = n;
        dfs();
        return ret;
    }
    void dfs()
    {
        if (right == num)
        {
            ret.push_back(path);
            return;
        }
        if (left < num)
        {
            path.push_back('(');
            left++;
            dfs();
            path.pop_back();
            left--;
        }
        if (right < left)
        {
            path.push_back(')');
            right++;
            dfs();
            path.pop_back();
            right--;
        }
    }
};

class Solution
{
    vector<string> hash = { "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };
    vector<string> ret;
    string path;
public:
    vector<string> letterCombinations(string digits)
    {
        if (digits.size() == 0) return {};
        dfs(digits, 0);
        return ret;
    }
    void dfs(string& digits, int pos)
    {
        if (pos == digits.size())
        {
            ret.push_back(path);
            return;
        }
        for (auto ch : hash[digits[pos] - '0'])
        {
            path.push_back(ch);
            dfs(digits, pos + 1);
            path.pop_back();
        }
    }
};

class Solution {
public:
    vector<vector<int>> ret;
    vector<int> path;
    bool check[7] = { false };
    vector<vector<int>> permute(vector<int>& nums)
    {
        dfs(nums);
        return ret;
    }

    void dfs(vector<int>& nums)
    {
        if (path.size() == nums.size())
        {
            ret.push_back(path);
            return;
        }
        for (int i = 0; i < nums.size(); i++)
        {
            if (check[i] == false)
            {
                path.push_back(nums[i]);
                check[i] = true;
                dfs(nums);
                check[i] = false;
                path.pop_back();
            }
        }
    }
};

class Solution
{
    vector<vector<int>> ret;
    vector<int> path;
    bool check[9] = { false };
public:
    vector<vector<int>> permuteUnique(vector<int>& nums)
    {
        sort(nums.begin(), nums.end());
        dfs(nums);
        return ret;
    }

    void dfs(vector<int>& nums)
    {
        if (path.size() == nums.size())
        {
            ret.push_back(path);
            return;
        }
        for (int i = 0; i < nums.size(); i++)
        {
            if (check[i] == true || (i != 0 && nums[i] == nums[i - 1] && check[i - 1] == false)) continue;
            path.push_back(nums[i]);
            check[i] = true;
            dfs(nums);
            path.pop_back();
            check[i] = false;
        }
    }
};

class Solution
{
    int sum = 0;
    int path = 0;
public:
    int subsetXORSum(vector<int>& nums)
    {
        dfs(nums, 0);
        return sum;
    }
    void dfs(vector<int>& nums, int pos)
    {
        for (int i = pos; i < nums.size(); i++)
        {
            path ^= nums[i];
            sum += path;
            dfs(nums, i + 1);
            path ^= nums[i];
        }
    }
};

// class Solution 
// {
//     vector<vector<int>> ret;
//     vector<int> path;
// public:
//     vector<vector<int>> subsets(vector<int>& nums) 
//     {
//         dfs(nums, 0);
//         return ret;
//     }m 
//     void dfs(vector<int>& nums, int i)
//     {
//         if(i == nums.size())
//         {
//             ret.push_back(path);
//             return;
//         }
//         dfs(nums, i+1);
//         path.push_back(nums[i]);
//         dfs(nums, i+1);
//         path.pop_back();
//     }
// };

class Solution
{
    vector<vector<int>> ret;
    vector<int> path;
public:
    vector<vector<int>> subsets(vector<int>& nums)
    {
        dfs(nums, 0);
        return ret;
    }
    void dfs(vector<int>& nums, int pos)
    {
        ret.push_back(path);
        for (int i = pos; i < nums.size(); i++)
        {
            path.push_back(nums[i]);
            dfs(nums, i + 1);
            path.pop_back();
        }
    }

};

class Solution {
public:
    vector<vector<int>> ret;
    vector<int> path;
    bool check[7] = { false };
    vector<vector<int>> permute(vector<int>& nums)
    {
        dfs(nums);
        return ret;
    }

    void dfs(vector<int>& nums)
    {
        if (path.size() == nums.size())
        {
            ret.push_back(path);
            return;
        }
        for (int i = 0; i < nums.size(); i++)
        {
            if (check[i] == false)
            {
                path.push_back(nums[i]);
                check[i] = true;
                dfs(nums);
                check[i] = false;
                path.pop_back();
            }
        }
    }
};

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
    vector<string> ret;
public:
    vector<string> binaryTreePaths(TreeNode* root)
    {
        string tmp;
        dfs(root, tmp);
        return ret;
    }
    void dfs(TreeNode* root, string tmp)
    {
        if (root == nullptr) return;
        if (root->left == nullptr && root->right == nullptr)
        {
            tmp += to_string(root->val);
            ret.push_back(tmp);
            return;
        }
        tmp += to_string(root->val);
        tmp += "->";
        if (root->left) dfs(root->left, tmp);
        if (root->right) dfs(root->right, tmp);
    }
};

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution
{
    int count = 0;
public:
    int kthSmallest(TreeNode* root, int k)
    {
        int ret = 0;
        dfs(root, k, &ret);
        return ret;
    }

    void dfs(TreeNode* root, int k, int* pret)
    {
        if (root == nullptr) return;
        dfs(root->left, k, pret);
        count++;
        if (count == k)
        {
            *pret = root->val;
            return;
        }
        dfs(root->right, k, pret);
    }
};




